package com.daiTest.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.daiTest.pojo.User;
import com.daiTest.service.UserService;
import com.daiTest.mapper.UserMapper;
import com.daiTest.untils.JwtHelper;
import com.daiTest.untils.MD5Util;
import com.daiTest.untils.Result;
import com.daiTest.untils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @description 针对表【news_user】的数据库操作Service实现
 * @createDate 2025-03-01 12:00:07
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    //mapper接口
    @Autowired
    private UserMapper userMapper;
    //Jwt工具
    @Autowired
    private JwtHelper jwtHelper;

    /*1根据账号 查询用户对象 -Login
     *2.如果用户对象为空 账号错误 501
     * 3.不为空 对比用户密码 失败 返回503错误
     *                    成功 根据用户id生成tonken 将token存给result->返回
     * */
    @Override
    public Result login(User user) {
        //1根据账号 查询用户对象 -Login
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, user.getUsername());
        User loginUser = userMapper.selectOne(lambdaQueryWrapper);

        if (loginUser == null) {
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }
        //对比密码
        if (!StringUtils.isEmpty(user.getUserPwd())
                && MD5Util.encrypt(user.getUserPwd()).equals(loginUser.getUserPwd())) {
            //登陆成功
            //根据用户id生成 token
            String token = jwtHelper.createToken(Long.valueOf(loginUser.getUid()));
            //将token 封装到result返回
            Map data = new HashMap();
            data.put("token", token);

            return Result.ok(data);
        }
        //密码错误 503
        return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
    }


    /*根据token获取用户数据
     *
     * 1.token是否在有效期 过期 504
     * 2.没过期 根据token解析出userId
     * 3.根据用户id查询用户数据
     * 4.去掉密码 封装result*/
    @Override
    public Result getUserInfo(String token) {
        //token是否在有效期
        boolean expiration = jwtHelper.isExpiration(token);
        if (expiration) {
            //过期 504 当做未登录看待
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        //没过期 根据token解析出userId
        int userId = jwtHelper.getUserId(token).intValue();
        //用userId查询用户数据
        User user = userMapper.selectById(userId);
        user.setUserPwd("");
        //把user装入Map中返回给Result
        Map data = new HashMap();
        data.put("loginUser", user);

        return Result.ok(data);
    }

    /*检查账号是否可用
    1.根据账号进行count查询
    count=0 说明用户名未被占用 可用
    count>0 说明用户名已被占用 不可用
    * */
    @Override
    public Result cheakUserName(String username) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(User::getUsername, username);
        Long count = userMapper.selectCount(lambdaQueryWrapper);
        if (count == 0) {
            return Result.ok(null);
        }
        return Result.build(null, ResultCodeEnum.USERNAME_USED);
    }

    /*注册账号
     * 1. 判断用户名是否被占用
     *       如果被占用 505
     *       未被占用 把数据添加到数据库中 200*/
    @Override
    public Result regist(User user) {
        //
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        Long count = userMapper.selectCount(queryWrapper);

        if (count > 0){
            return Result.build(null,ResultCodeEnum.USERNAME_USED);
        }
        user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
        int rows = userMapper.insert(user);
        //System.out.println("rows = " + rows);
        return Result.ok(null);
}

    /*检验token是否过期
     * */
    @Override
    public Result checkLogin(String token) {
        boolean expiration = jwtHelper.isExpiration(token);
        //过期
        if(StringUtils.isEmpty(token)||expiration){
           return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }

        return Result.ok(null);
    }




}




